<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog :title="!dataForm.id ? '添加-ADD' : '修改-EDITE'" :close-on-click-modal="false" :visible.sync="visible">
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="删除标识 1是" prop="isDel">
        <el-input v-model="dataForm.isDel" placeholder="删除标识 1是"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/contactus.js'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        phone: '',
        email: '',
        content: '',
        isDel: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        name: [
          { required: true, message: '姓名  为必填项', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机  为必填项', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱  为必填项', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容  为必填项', trigger: 'blur' }
        ],
        isDel: [
          { required: true, message: '删除标识 1是  为必填项', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间  为必填项', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间  为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) { // 初始化表单验证规则
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          api.contactusDetailApi(id).then(res => {
            this.dataForm = res;
          })
        }
      })
    },
    // 表单数据提交
    dataSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          api.ContactUsCreateApi().then(res => {
            // TODO 保存数据
          });
        }
      })
    }
  }
}
</script>
../../../api/contactus.js